<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../js/vue/vue.js"></script>
    <script src="../../js/jquery/jquery.js"></script>
    <script src="../../js/jquery-form/jquery.form.js"></script>
</head>
<body>

<!-- 页面模板 -->
<div id="app">
    <form id="formId">
        <input type="hidden" name="id" v-model="emp.id"><br>
        名称: <input type="text" name="name" v-model="emp.name"><br>
        年龄: <input type="text" name="age" v-model="emp.age"><br>
        <input type="button" id="btn" @click="submitForm()" value="提交">
    </form>
</div>
<script>
    //http://localhost:8088/views/demo/input.html?id=1&name=?
    //获取url上的请求参数
    function getParams() {
        //获取问号及问号后面的内容
        //?id=1&name=dafei
        var url = window.location.search;
        var params = new Object();
        if (url.indexOf("?") != -1) {
            //截取问号后面的内容,再使用&分割多个属性
            var arr = url.substr(1).split("&"); //[id=1,name=dafei]
            for (var i = 0; i < arr.length; i++) {
                //使用=分割为keyvalue
                //id=1
                //[id,1] [name,dafei]
                var keyValue = arr[i].split("=");
                //[id,1]
                params[keyValue[0]] = keyValue[1];
            }
        }
        return params;
    }
    console.log(getParams());
    var vue = new Vue({
        el: '#app', //获取页面模板 通过上面的id指定
        data: { //vue 渲染模板的时候需要的数据
            emp:{}
        },
        methods: {
            submitForm: function () {

                //$("#formId").ajaxSubmit(); 将表单转换成异步表单,并提交
                //$("#formId").ajaxForm(); 将表单转换成异步表单,不提交
                $("#formId").ajaxSubmit({
                    url: "http://localhost:8088/emps/saveOrUpdate",
                    type: "POST",
                    success: function (data) {
                        //访问成功做什么
                        console.log(data);
                        if (data.success){
                            //成功
                            window.location.href = "list.html";
                        }else {
                            //失败
                            alert(data.msg);
                        }
                    }
                });
            }
        },
        mounted: function () {
            //获取员工id,异步查询员工信息
            var id = getParams().id;
            //id有值才做编辑
            if (id) {
                var _this = this;
                $.get("http://localhost:8088/emps/detail", {id: id}, function (data) {
                    console.log(data);
                    _this.emp = data;
                })
            }
        }
    });
</script>
</body>
</html>